package com.pan.test.udp.example01;

import java.io.IOException;
import java.net.*;

public class Receiver02 {
    /**
     * 发送信息
     */
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(11452);

        String msg = "原神，启动";
        int len = msg.getBytes().length;
        InetAddress inetAddress = InetAddress.getByName("192.168.31.43");
        DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),len,inetAddress,11451);
        datagramSocket.send(datagramPacket);


        byte[] data = new byte[1024];
        DatagramPacket datagramPacket2 = new DatagramPacket(data,0,data.length);
        datagramSocket.receive(datagramPacket2);
        System.out.println("接收: "+new String(datagramPacket2.getData(),0,datagramPacket2.getLength()));


        datagramSocket.close();
    }
}
